package com.platform.api;

import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.ApiLabelVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiLabelService;
import com.platform.service.ApiUserLabelService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author zhurusen <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@RestController
@RequestMapping("/api/label")
public class ApiLabelController extends ApiBaseAction {

    @Autowired
    private ApiLabelService apiLabelService;
    @Autowired
    private ApiUserLabelService apiUserLabelService;

    @RequestMapping("list")
    public R zbList(@LoginUser UserVo loginUser) {
        Map<String, Object> params = new HashMap();

        List<ApiLabelVo> list = apiLabelService.queryList(params);
        return R.ok(0, "获取成功", list);
    }


    /**
     * 主播的i标签
     *
     * @param userId
     * @return
     */
    @IgnoreAuth
    @RequestMapping("listByUserId")
    public R zbList(Long userId) {
        Map<String, Object> params = new HashMap();
        params.put("zbUserId", userId);
        params.put("mould_name", "userVo");
        List<Map<String, Object>> list = apiUserLabelService.selectMap(params);

        if (list.size() == 0) {
            params = new HashMap();
            params.put("zbUserId", userId);
            params.put("page", 1);
            params.put("limit", 2);
            params.put("sidx", "id");
            params.put("order", "desc");
            Query query = new Query(params);
            list = apiUserLabelService.selectMap(query);
        }


        for (Map<String, Object> map : list) {
            Map<String, Object> param = new HashMap();
            param.put("name", map.get("name"));
            List<ApiLabelVo> labelVos = apiLabelService.queryList(param);
            if (labelVos != null && labelVos.size() > 0) {
                map.put("id", labelVos.get(0).getId());
            } else {
                map.put("id", 1);
            }
        }
        return R.ok(0, "获取成功", list);
    }
}